import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text("CustomScrollView"),
      ),
      body: HomeScreen(),
    ),
  ));
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  ScrollController _controller;

  @override
  void initState() {
    _controller = new ScrollController(initialScrollOffset: 100);
    _controller.addListener(() {
      print(
          "正在滚动:当前滚动位置${_controller.offset},${_controller.position.pixels},最大滚动距离${_controller.position.maxScrollExtent}");
    });

    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Container(
            color: Colors.pink,
            child: CustomScrollView(
              scrollDirection: Axis.vertical,
              reverse: false,
              controller: _controller,
              physics: BouncingScrollPhysics(),
              slivers: <Widget>[
                SliverAppBar(
                  //automaticallyImplyLeading: false,
                  title: Text("我的AppBar"),
                  pinned: true,
                  expandedHeight: 200.0,
                  flexibleSpace: FlexibleSpaceBar(
                    title: Text("flex space bar title"),
                    titlePadding: EdgeInsets.all(0),
                    collapseMode: CollapseMode.parallax,
                    background: Image.network(
                      "http://f.hiphotos.baidu.com/image/h%3D300/sign=d8d48aa8a151f3dedcb2bf64a4eff0ec/4610b912c8fcc3ce863f8b519c45d688d53f20d0.jpg",
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                SliverPadding(
                  padding: EdgeInsets.all(8.0),
                  sliver: SliverList(
                    delegate: SliverChildListDelegate(
                      <Widget>[
                        Container(
                          width: double.infinity,
                          height: 400,
                          color: Colors.black,
                        ),
                        Container(
                          width: double.infinity,
                          height: 400,
                          color: Colors.yellow,
                        ),
                        Container(
                          width: double.infinity,
                          height: 400,
                          color: Colors.green,
                        ),
                      ],
                    ),
                  ),
                ),
                SliverGrid(
                    delegate: SliverChildListDelegate(<Widget>[
                      Container(
                        width: double.infinity,
                        height: 400,
                        color: Colors.deepOrange,
                      ),
                      Container(
                        width: double.infinity,
                        height: 400,
                        color: Colors.purple,
                      ),
                      Container(
                        width: double.infinity,
                        height: 400,
                        color: Colors.cyan,
                      ),
                      Container(
                        width: double.infinity,
                        height: 400,
                        color: Colors.green,
                      ),
                      Container(
                        width: double.infinity,
                        height: 400,
                        color: Colors.yellow,
                      ),
                      Container(
                        width: double.infinity,
                        height: 400,
                        color: Colors.red,
                      )
                    ]),
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 3,
                      mainAxisSpacing: 8.0,
                      crossAxisSpacing: 12.0,
                      childAspectRatio: 0.4,
                    )),
              ],
            )),
        Positioned(
          bottom: 10,
          left: 10,
          child: RaisedButton(
            child: Text("滚动到指定位置无动画"),
            onPressed: () {
              _controller.jumpTo(Random()
                  .nextInt(_controller.position.maxScrollExtent.floor())
                  .toDouble());
            },
          ),
        ),
        Positioned(
          bottom: 100,
          right: 10,
          child: RaisedButton(
            child: Text("滚动到指定位置带动画"),
            onPressed: () {
              _controller.animateTo(
                  Random()
                      .nextInt(_controller.position.maxScrollExtent.floor())
                      .toDouble(),
                  duration: Duration(milliseconds: 300),
                  curve: Curves.easeIn);
            },
          ),
        ),
      ],
    );
  }
}
